package com.example.redisrabbitesintegeation.mq;

import com.example.redisrabbitesintegeation.common.config.RabbitConfig;
import com.example.redisrabbitesintegeation.model.entity.News;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @ClassName Producer
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/21 19:18
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor
public class Producer {

    private final RabbitTemplate rabbitTemplate;

    public void send(News news) {
        // 创建消息唯一ID
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        // 投递消息
        rabbitTemplate.convertAndSend(RabbitConfig.NEWS_EXCHANGE, RabbitConfig.NEWS_CREATE_ROUTING_KEY, news, correlationData);
    }

    public void send(String newsId) {
        // 创建消息唯一ID
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        // 投递消息
        rabbitTemplate.convertAndSend(RabbitConfig.NEWS_EXCHANGE, RabbitConfig.NEWS_DELETE_ROUTING_KEY, newsId, correlationData);
    }
}
